<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/10/20
 * Time: 13:36
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');
class UserController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->question = new QuestionModel();
        $this->user = new UserModel();
        $this->getView()->assign("imgPath", HTTP_WEB.VERSION_LIVESTAR.'/Default/');
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    public function siteAction()
    {
        $this->getView()->assign("title", '个人设置');
    }

    public function liveAction()
    {
        $page=$this->getRequest()->get("page");
        $live = $this->user->liveAction($this->authentication['data'],$page);
        $this->getView()->assign("live", $live);
        $this->getView()->assign("title", '视频');

    }

    public function delLiveAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
            ]);
            $validate->message([
                'llid.require' => '培训不能为空',
                'llid.number' => '培训类型错误',
                'llid.egt' => '培训错误',
                'llid.exist' => '培训不存在',
            ]);
            if ($validate->check($content)) {
                $llid = $content['llid'];
                $code = 200;
                $data = $this->user->delLiveAction($this->authentication['data'],$llid);
                if(!empty($data['code'])){
                    $code = $data['code'];
                    unset($data['code']);
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function meetingAction()
    {
        $page=$this->getRequest()->get("page");
        $meeting = $this->user->meetingAction($this->authentication['data'],$page);
        $this->getView()->assign("meeting", $meeting);
        $this->getView()->assign("title", '会议');

    }

    public function delMeetingAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'mid' => 'require|number|egt:1|exist:meeting,id',
            ]);
            $validate->message([
                'mid.require' => '培训不能为空',
                'mid.number' => '培训类型错误',
                'mid.egt' => '培训错误',
                'mid.exist' => '培训不存在',
            ]);
            if ($validate->check($content)) {
                $mid = $content['mid'];
                $code = 200;
                $data = $this->user->delMeetingAction($this->authentication['data'],$mid);
                if(!empty($data['code'])){
                    $code = $data['code'];
                    unset($data['code']);
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function sitePostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'nickname' => 'chsDash|min:1|max:16',
                'section' => 'chsAlphaNum|min:1|max:16',
                'duties' => 'chsAlphaNum|min:1|max:16',
                'sex' => 'number|between:0,3',
            ]);
            $validate->message([
                'nickname.chsDash' => '昵称只能是汉字、字母、数字和下划线_及破折号-',
                'nickname.min' => '昵称不能小于2个字符',
                'nickname.max' => '昵称不能大于16个字符',
                'section.chsAlphaNum' => '部门只能是汉字、字母和数字',
                'section.min' => '部门不能小于2个字符',
                'section.max' => '部门不能大于16个字符',
                'duties.chsAlphaNum' => '职务只能是汉字、字母和数字',
                'duties.min' => '职务不能小于2个字符',
                'duties.max' => '职务不能大于16个字符',
                'sex.number' => '性别错误',
                'sex.min' => '性别不在范围内',
            ]);
            if ($validate->check($content)) {
                    $code = 200;
                    $data = [];
                    if(!empty($content['nickname'])){
                        $data['nickname'] = $content['nickname'];
                    }
                    if(!empty($content['section'])){
                        $data['section'] = $content['section'];
                    }
                    if(!empty($content['duties'])){
                        $data['duties'] = $content['duties'];
                    }
                    if(!empty($content['sex'])){
                        $data['sex'] = $content['sex'];
                    }
                    if(!empty($content['path']) && !empty($content['file'])){
                        $path = $content['path'];
                        $file = $content['file'];
                    }else{
                        $path = 0;
                        $file = 0;
                    }
                    $data = $this->user->editAction($data,$this->authentication['data']['id'],$path,$file);
                    $redis = Yaf_Registry::get('redis');
                    $jsToken = json_decode($redis->hget('User',$this->authentication['data']['phone']),true);
                    unset($jsToken['data']);
                    $jsToken['data'] = $data['data'];
                    $redis->hset('User',$this->authentication['data']['phone'], json_encode($jsToken));
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function resultAction()
    {
        $myRanking = $this->question->myRankingAction($this->authentication['data']);
        $this->getView()->assign("myRanking", $myRanking);
        $this->getView()->assign("title", '我的成绩');
    }

    public function recordAction()
    {
        $page=$this->getRequest()->get("page");
        $record = $this->user->recordAction($this->authentication['data']['id'],$page);
        $this->getView()->assign("record", $record);
        $this->getView()->assign("title", '充值记录');
    }
}